<template>
  <div class="container">
    <div style="width: 25%; display: flex; background-color: white;">
      <div
          style="flex: 1; width: 50%; padding: 40px; display: flex; flex-direction: column; justify-content: center;"
      >
        <div
            style="text-align: center; font-size: 30px; color: #131313; margin-bottom: 15px;"
        >
          仓 库 管 理 系 统
        </div>

        <!-- 登录表单 -->
        <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules" label-width="0">
          <el-form-item prop="num">
            <el-input
                v-model="loginForm.num"
                placeholder="请输入账号"
                style="width: 100%;"
                autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                style="width: 100%;"
                autocomplete="off"
                @keyup.enter.native="login"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="login" style="width: 100%; margin-top: 30px"
            >登录</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button type="text" @click="registerDialogVisible = true" style="width: 100%; color: #409EFF"
            >注册新用户</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 注册弹窗 -->
    <el-dialog
        title="用户注册"
        :visible.sync="registerDialogVisible"
        width="400px"
        :before-close="handleRegisterDialogClose"
    >
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="90px">
        <el-form-item label="账号" prop="num">
          <el-input v-model="registerForm.num" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-select v-model="registerForm.gender" placeholder="请选择性别">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="registerForm.age" :min="1" :max="120" placeholder="请输入年龄"></el-input-number>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="registerForm.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleRegisterDialogClose">取消</el-button>
        <el-button type="primary" @click="register">注册</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        num: "",
        password: "",
      },
      registerForm: {
        num: "",
        password: "",
        confirmPassword: "",
        name: "",
        gender: null,
        age: null,
        phone: "",
      },
      registerDialogVisible: false,
      loginRules: {
        num: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      registerRules: {
        num: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 12, message: "密码长度3-12位", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value !== this.registerForm.password) {
                callback(new Error("两次密码输入不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        phone: [
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return;

        this.$axios
            .get(
                this.$httpUrl +
                "/user/login?num=" +
                this.loginForm.num +
                "&password=" +
                this.loginForm.password
            )
            .then((res) => res.data)
            .then((res) => {
              if (res.code === 200) {
                sessionStorage.setItem("CurUser", JSON.stringify(res.data.user));
                this.$store.commit("setMenu", res.data.menu);
                this.$router.replace("/index");
              } else {
                this.$message.error("账号或密码错误");
              }
            })
            .catch(() => {
              this.$message.error("网络异常，请稍后重试");
            });
      });
    },
    register() {
      this.$refs.registerForm.validate((valid) => {
        if (!valid) return;

        // 调用注册接口
        this.$axios
            .post(this.$httpUrl + "/user/register", {
              num: this.registerForm.num,
              password: this.registerForm.password,
              name: this.registerForm.name,
              sex: this.registerForm.gender,  // 这里改名
              age: this.registerForm.age,
              phone: this.registerForm.phone,
            })
            .then((res) => res.data)
            .then((res) => {
              if (res.code === 200) {
                this.$message.success("注册成功，请登录");
                this.registerDialogVisible = false;
                this.resetRegisterForm();
              } else {
                this.$message.error(res.message || "注册失败");
              }
            })
            .catch(() => {
              this.$message.error("网络异常，请稍后重试");
            });
      });
    },
    resetRegisterForm() {
      this.$refs.registerForm.resetFields();
    },
    handleRegisterDialogClose() {
      this.registerDialogVisible = false;
      this.resetRegisterForm();
    },
  },
};
</script>

<style>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("../assets/bg1.jpg");
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
.login-form {
  margin-top: 20px;
}
</style>